<import file="block/common.html" name="url" :tpl_cfg="['urlName'=>'内容列表']"/>
<div class="layui-fluid">
	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
		<script type="text/html" id="toolbarAE">
            <div class="layui-form">
				<?php if(SysRolCk("content_add")){echo "<button class='layui-btn layui-btn-sm  data-add-btn'><i class='layui-icon'>&#xe61f;</i>".lang('add')."</button>"; }?>
				<div class="layui-inline layui-show-xs-block" >
					<input type="text" name="title" id="title" placeholder="按标题搜索" class="layui-input">
				</div>
				<div class="layui-inline layui-show-xs-block"  style="width:160px;">
					<select name="searchcid" id="searchcid" lay-verify="">
						<option value="0">所有栏目</option>
						<?php foreach($catelist as $key=>$vo){?>
						  <option value="<?php echo $vo['id'];?>" <?php echo $_GET['cid'] == $vo['id']?'selected=""':''; echo $vo['type']==0 ?'disabled=""':''; ?>> <?php echo $vo['cname'];?></option>
						<?php }?>  
					</select>
				</div>
				<div class="layui-inline layui-show-xs-block" style="width:80px;">
					<select id="stat" name="stat" lay-verify="required">
						<option value="1">已审</option>
						<option value="0">草稿</option>
					</select>
				</div>
				<div class="layui-inline layui-show-xs-block">
					<button class="layui-btn layui-btn-sm " data-type="reload" id="searchBtn"><i class="layui-icon">&#xe615;</i>查询</button>
				</div>
				<div class="layui-inline layui-show-xs-block" style="color: aliceblue; width: 40px;height: 35px;"> | </div>
				<?php if(SysRolCk("content_del")){ ?>
					<button class='layui-btn layui-btn-sm  layui-btn-danger data-batchdel-btn'><i class="layui-icon layui-icon-delete"></i> 批量删除 </button>
				<?php }?>
				<?php if(SysRolCk("content_edit")){ ?>
				<div class="layui-inline layui-show-xs-block" style="width:160px;">
					<select name="transfercid" id="transfercid" lay-verify="">
						<?php foreach($catelist as $key=>$vo){?>
						  <option value="<?php echo $vo['id'];?>" <?php echo $vo['type']==0 ?'disabled=""':''; ?>> <?php echo $vo['cname'];?></option>
						<?php }?>  
					</select>
				</div>
				<button class='layui-btn layui-btn-sm  data-batchtransfer-btn'><i class="layui-icon layui-icon-transfer"></i> 批量转移 </button>
				<?php } ?>
			</div>
        </script>
		<table class="layui-hide" id="currentTableId" lay-data="{id: 'tableData'}" lay-filter="currentTableFilter"></table>

		<script type="text/html" id="cateUrlTpl"><a href="/list/{{d.cid}}" target="_blank">{{d.catename}}</a></script>
		<script type="text/html" id="contentUrlTpl">
			<a href="/info/{{d.id}}" target="_blank">{{d.title}}</a>			
		</script>
		<script type="text/html" id="contentUserTpl">{{d.nickname}}</script>
		<script type="text/html" id="contentGroupTpl">{{d.groupname}}</script>
		
		<script type="text/html" id="switchTpl">
			<?php if(SysRolCk("content_status")){ ?>
				<input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="审|审" lay-filter="switchStatus" {{ d.status == 1 ? "checked" : "" }}>
			<?php }else{ ?>
				{{#  if(d.status == 1){ }}<span class='layui-badge layui-bg-green'>已审</span>{{#  }else{ }} <span class='layui-badge layui-bg-red'>未审</span> {{# } }}
			<?php } ?>

			<?php if(SysRolCk("content_attribute")){ ?>
				<input type="checkbox" name="attribute_a" id="{{d.id}}" value="{{d.attribute_a}}" lay-skin="switch" lay-text="头|头" lay-filter="switchStatus" {{ d.attribute_a == 1 ? "checked" : "" }}>
				<input type="checkbox" name="attribute_b" id="{{d.id}}" value="{{d.attribute_b}}" lay-skin="switch" lay-text="小|小" lay-filter="switchStatus" {{ d.attribute_b == 1 ? "checked" : "" }}>
				{{#  if(d.imgbl == 1){ }} <input type="checkbox" name="attribute_c" id="{{d.id}}" value="{{d.attribute_c}}" lay-skin="switch" lay-text="轮|轮" lay-filter="switchStatus" {{ d.attribute_c == 1 ? "checked" : "" }}>{{#  } }}
			<?php }else{ ?>
				{{#  if(d.imgbl == 1){ }}<i class='layui-icon layui-icon-picture' style='color:#1E9FFF'></i>{{#  } }}				
				{{#  if(d.attribute_a == 1){ }}<span class='layui-badge layui-bg-blue'>头条</span>{{#  } }}
				{{#  if(d.attribute_b == 1){ }}<span class='layui-badge layui-bg-blue'>小头条</span>{{#  } }}
				{{#  if(d.attribute_c == 1){ }}<span class='layui-badge layui-bg-blue'>轮换</span>{{#  } }}
			<?php } ?>
		</script>
		
	<script type="text/html" id="currentTableBar">
			<div class="layui-btn-group">
		<?php echo SysRolCk("content_edit")?'<a class="layui-btn layui-btn-sm data-count-edit" lay-event="edit"><i class="layui-icon">&#xe642;</i>'.lang('edit').'</a>':''; ?>
		<?php echo SysRolCk("content_del")?'<a class="layui-btn  layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon">&#xe640;</i>'.lang('del').'</a>':''; ?>
			</div>
	</script>
		</div>
	</div>
</div>
<script>
	layui.define(function () {
		var table = layui.table,
			$ = layui.jquery,
			layer = layui.layer,
			form = layui.form;

		form.render();
		table.render({
			id: 'tableData',
			elem: '#currentTableId',
			url: '<{CAUrl("content","index")}>&cid=<{$cid}>&list_json',
			toolbar: '#toolbarAE',		
			cols: [[
				{type: "checkbox", width: 50, fixed: "left"},
				{ field: 'id', width: 80, title: 'ID', sort: true },
				{ field: 'catename', width: 120, title: '栏目', templet: '#cateUrlTpl' },
				{ field: 'title', title: '标题', templet: '#contentUrlTpl' },
				{ field: 'nickname', width: 100, title: '发布人', templet: '#contentUserTpl' },
				{ field: 'groupname', width: 100, title: '发布角色', templet: '#contentGroupTpl' },
				{ field: 'time', width: 200, title: '发布时间', sort: true },
				{ field: 'status', width: 260, title: '状态', templet: '#switchTpl', unresize: true },
				{ title: '操作', width: 200, templet: '#currentTableBar', fixed: "right", align: "center" }
			]],
			page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
				layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
				//,curr: 5 //设定初始在第 5 页
				, groups: 1 //只显示 1 个连续页码
				, first: false //不显示首页
				, last: false //不显示尾页
			},
			limits: [10, 15, 20, 25, 50, 100],
			limit: 15,	
			//pagebar:'#pagebarAE',
			page: true,			
		});

		// 监听添加操作
		$(".data-add-btn").on("click", function () {
			loadMain('<{CAUrl("content","add")}>&view&cid=<?php echo $_GET["cid"];?>');
			return false;
		});

		//监听状态操作
		form.on('switch(switchStatus)', function (obj) {
			$.post('<{CAUrl("content","edit")}>&switchStatus',{ id: this.id, status: obj.elem.checked?1:0,att:obj.elem.name},function (res) {
				layer.tips(res.info,obj.othis,{tips: 1});});
		});


		table.on('tool(currentTableFilter)', function (obj) {
			var data = obj.data;
			if (obj.event === 'edit') {
				loadMain('<{CAUrl("content","edit")}>&view&id=' + data.id);
				return false;
			} else if (obj.event === 'delete') {
				layer.confirm('删除 ID:'+data.id+' '+data.title+' ?', function (index) {
					$.post('<{CAUrl("content","del")}>', {id:data.id}, function (res) {
						if (res.status == 1) {
							layer.msg(res.info, { icon: 1, time: 1000 }, function () {
								obj.del();
								layer.close(index);
							});
						} else {
							layer.msg(res.info, { icon: 2, anim: 6, time: 2000 });
						}
					});
				});
			}
		});

		//搜索重载表格
		$('#searchBtn').on('click', function() {
			table.reloadData('tableData', {
				method : 'get',
				where : {
					title : $('#title').val(),
					searchcid : $('#searchcid').val(),
					stat: $('#stat').val(),
				},
				page: {
					curr: 1
				},
				page : true,
			});
			return false;
		});


		//获取多选框ID值
		var checkIdFun = function(){
			var checkStatus = table.checkStatus('tableData');
			var data = checkStatus.data;
			var arr = new Array();
			for(var i = 0;i<data.length;i++){
				arr.push(data[i]['id']);
			}
			return arr;
		}

		// 批量转移栏目操作
		$(".data-batchtransfer-btn").on("click", function () {
			if(checkIdFun().length <1){
				layer.msg('没有选择内容');
				return false;
			}				
			layer.confirm('确认转移 ID: '+checkIdFun()+' 的内容信息？', function (index) {
				$.post('<{CAUrl("content","edit")}>&batchtransfer=1', {id:checkIdFun,transfercid:$('#transfercid').val()}, function (res) {
						layer.msg(res.info, { icon: 1, time: 2000 }, function () {
							table.reloadData('tableData', {});
							layer.close(index);
						});
				});
			});
			return false;
		});


		// 批量删除操作
		$(".data-batchdel-btn").on("click", function () {
			if(checkIdFun().length <1){
				layer.msg('没有选择内容');
				return false;
			}
			layer.confirm('确认删除 ID: '+checkIdFun()+' 的内容信息？', function (index) {
				$.post('<{CAUrl("content","del")}>', {id:checkIdFun}, function (res) {
					if (res.status == 1) {
						layer.msg(res.info, { icon: 1, time: 2000 }, function () {
							table.reloadData('tableData', {});
							layer.close(index);
						});
					} else {
						layer.msg(res.info, { icon: 2, anim: 6, time: 2000 });
					}
				});
			});
			return false;
		});

	});
</script>